package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 机构认证审核表(AgencyCertificationAudit)表服务实现类
 *
 * @author makejava
 * @since 2025-10-08 01:39:16
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertificationAuditService {
    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;
    @Resource
    private WorkerCertificationMapper workerCertificationMapper;


    @Override
    public void submit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        WorkerCertificationAudit bean =
                BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
        //待审核
        bean.setAuditStatus(0);
        workerCertificationAuditMapper.insert(bean);
//        认证中或者认证过的的会在认证表中有记录
        LambdaQueryWrapper<WorkerCertification> workerCertificationLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<WorkerCertification> query = workerCertificationLambdaQueryWrapper.eq(WorkerCertification::getId,
                workerCertificationAuditAddReqDTO.getServeProviderId());

        WorkerCertification workerCertification = workerCertificationMapper.selectOne(query);

        if (!(Objects.isNull(workerCertification))) {
            //2.将认证信息状态更新为认证中
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());//认证中
            workerCertificationMapper.updateById(workerCertification);

        }else
        {
            WorkerCertification workerCertification1 = new WorkerCertification();
            workerCertification1.setId(workerCertificationAuditAddReqDTO.getServeProviderId());
            workerCertification1.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationMapper.insert(workerCertification1);

        }


    }

    @Override
    public RejectReasonResDTO lookRejectReason() {
        Long id = UserContext.currentUser().getId();
        LambdaQueryWrapper<WorkerCertificationAudit> workerCertificationAuditLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        //注意服务端和机构端会请求多个机构认证信息，所以这里要限制
        LambdaQueryWrapper<WorkerCertificationAudit> query = workerCertificationAuditLambdaQueryWrapper
                .eq(WorkerCertificationAudit::getServeProviderId, id)
                .eq(WorkerCertificationAudit::getCertificationStatus, 3)
//                注意这个写法，注意这里要写limit 1
                .orderByDesc(WorkerCertificationAudit::getCreateTime)
                .last("limit 1");

        WorkerCertificationAudit workerCertificationAudit =
                workerCertificationAuditMapper.selectOne(query);

        return new RejectReasonResDTO(workerCertificationAudit.getRejectReason());

    }

    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageAudit(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        return PageHelperUtils.selectPage(workerCertificationAuditPageQueryReqDTO,
                ()->workerCertificationAuditMapper
                        .pageWorkerCertificationAudit(workerCertificationAuditPageQueryReqDTO));
    }

    @Override
    public void audit(Long certificationStatus, Long id, String rejectReason) {
        if (Objects.equals(certificationStatus,3L)&&Objects.isNull(rejectReason)) {
            throw new RuntimeException("请填写拒绝理由");
        }
        LambdaUpdateWrapper<WorkerCertificationAudit> workerCertificationAuditLambdaUpdateWrapper
                = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<WorkerCertificationAudit> query = workerCertificationAuditLambdaUpdateWrapper
                .eq(WorkerCertificationAudit::getId, id)
                .set(WorkerCertificationAudit::getCertificationStatus, certificationStatus)
                .set(ObjectUtil.isNotNull(rejectReason), WorkerCertificationAudit::getRejectReason, rejectReason);
        workerCertificationAuditMapper.update(null, query);



    }
}

